package com.cws.project.cws.course.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.cws.common.utils.DateUtils;
import com.cws.common.utils.StringUtils;
import com.cws.common.utils.text.Convert;
import com.cws.project.cws.resitscore.domain.CourseResitScore;
import com.cws.project.cws.resitscore.mapper.CourseResitScoreMapper;
import com.cws.project.cws.score.domain.HomeworkScore;
import com.cws.project.cws.score.mapper.HomeworkScoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cws.project.cws.course.mapper.CourseMapper;
import com.cws.project.cws.course.domain.Course;
import com.cws.project.cws.course.service.ICourseService;

/**
 * 课程信息Service业务层处理
 *
 * @author xyl
 * @date 2022-01-04
 */
@Service
public class CourseServiceImpl implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private HomeworkScoreMapper homeworkScoreMapper;
    @Autowired
    private CourseResitScoreMapper courseResitScoreMapper;

    /**
     * 查询课程信息
     *
     * @param courseId 课程信息ID
     * @return 课程信息
     */
    @Override
    public Course selectCourseById(Long courseId) {
        return courseMapper.selectCourseById(courseId);
    }

    /**
     * 查询课程信息列表
     *
     * @param course 课程信息
     * @return 课程信息
     */
    @Override
    public List<Course> selectCourseList(Course course) {
        return courseMapper.selectCourseList(course);
    }

    /**
     * 新增课程信息
     *
     * @param course 课程信息
     * @return 结果
     */
    @Override
    public int insertCourse(Course course) {
        course.setCreateTime(DateUtils.getNowDate());
        return courseMapper.insertCourse(course);
    }

    /**
     * 修改课程信息
     *
     * @param course 课程信息
     * @return 结果
     */
    @Override
    public int updateCourse(Course course) {
        course.setUpdateTime(DateUtils.getNowDate());
        return courseMapper.updateCourse(course);
    }

    /**
     * 删除课程信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(String ids) {
        return courseMapper.deleteCourseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程信息信息
     *
     * @param courseId 课程信息ID
     * @return 结果
     */
    @Override
    public int deleteCourseById(Long courseId) {
        return courseMapper.deleteCourseById(courseId);
    }

    @Override
    public List<Course> selectStudentCourseList(Course course) {
        List<Course> courseList = courseMapper.selectStudentCourseList(course);
        for (Course course1 : courseList) {
            if (StringUtils.equals("是", course1.getIsCourseEnd())) {
                Double score = course1.getScore();
                //判断是否及格
                if(score < 60){
                    //查询补考成绩 。 如果查询到 ， 就覆盖。 如果没有就不覆盖
                    CourseResitScore crs = courseResitScoreMapper.selectCourseResitScore(course.getStudentId(),course1.getCourseId());
                    if(crs != null){
                        score = crs.getScore();
                    }
                }
                course1.setScore(score);
                //计算gpa
                BigDecimal b = new BigDecimal(score * 4 / 100);
                course1.setGpa(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            }
        }
        return courseList;
    }
}
